package com.dongdong.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongdong.constant.ContentConstantRedis;
import com.dongdong.mapper.TbContentMapper;
import com.dongdong.pojo.TbContent;
import com.dongdong.service.ContentService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Deacription TODO
 * @Author Lenovo
 * @Date 2020/12/16 11:46
 * @Version 1.0
 **/
@Component
@Service
public class ContentServiceImpl implements ContentService {

    @Resource
    private TbContentMapper tbContentMapper;

    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public List<TbContent> getContentCategory() {
        return tbContentMapper.selectByExample(null);
    }

    @Override
    public void insertContent(TbContent content) {
        tbContentMapper.insertSelective(content);
    }

    @Override
    public TbContent toUpdate(Long id) {
        return tbContentMapper.selectByPrimaryKey(id);
    }

    //楼层区
    @Override
    public List<TbContent> getFloorCarousels() {
        List<TbContent> contents = null;
        if (!redisTemplate.opsForHash().hasKey(ContentConstantRedis.FLOORCAROUSELS,ContentConstantRedis.FIELD)){
            contents = tbContentMapper.getFloorCarousels();
            redisTemplate.opsForHash().put(ContentConstantRedis.FLOORCAROUSELS,ContentConstantRedis.FIELD,contents);
            //设置失效时间
            redisTemplate.expire(ContentConstantRedis.LIKE,1, TimeUnit.HOURS);
        }else {
            contents = (List<TbContent>)redisTemplate.opsForHash().get(ContentConstantRedis.FLOORCAROUSELS,ContentConstantRedis.FIELD);
        }
        return contents;
    }

    //有趣区
    @Override
    public List<TbContent> getAmusing() {
        List<TbContent> contents = null;
        if (!redisTemplate.opsForHash().hasKey(ContentConstantRedis.AMUSING,ContentConstantRedis.FIELD)){
            contents = tbContentMapper.getAmusing();
            redisTemplate.opsForHash().put(ContentConstantRedis.AMUSING,ContentConstantRedis.FIELD,contents);
            //设置失效时间
            redisTemplate.expire(ContentConstantRedis.LIKE,1, TimeUnit.HOURS);
        }else {
            contents = (List<TbContent>)redisTemplate.opsForHash().get(ContentConstantRedis.AMUSING,ContentConstantRedis.FIELD);
        }
        return contents;
    }

    //猜你喜欢
    @Override
    public List<TbContent> getLike() {
        List<TbContent> contents = null;
        if (!redisTemplate.opsForHash().hasKey(ContentConstantRedis.LIKE,ContentConstantRedis.FIELD)){
            contents = tbContentMapper.getLike();
            redisTemplate.opsForHash().put(ContentConstantRedis.LIKE,ContentConstantRedis.FIELD,contents);
            //设置失效时间
            redisTemplate.expire(ContentConstantRedis.LIKE,1, TimeUnit.HOURS);
        }else {
            contents = (List<TbContent>)redisTemplate.opsForHash().get(ContentConstantRedis.LIKE,ContentConstantRedis.FIELD);
        }
        return contents;
    }

    //轮播图
    @Override
    public List<TbContent> getCarousel() {
        List<TbContent> contents = null;
        if (!redisTemplate.opsForHash().hasKey(ContentConstantRedis.CAROUSEL,ContentConstantRedis.FIELD)){
            contents = tbContentMapper.getCarousel();
            redisTemplate.opsForHash().put(ContentConstantRedis.CAROUSEL,ContentConstantRedis.FIELD,contents);
            //设置失效时间
            redisTemplate.expire(ContentConstantRedis.CAROUSEL,1, TimeUnit.HOURS);
        }else {
            contents = (List<TbContent>)redisTemplate.opsForHash().get(ContentConstantRedis.CAROUSEL,ContentConstantRedis.FIELD);
        }
        return contents;
    }

    //今日推荐
    @Override
    public List<TbContent> getCommend() {
        List<TbContent> contents = null;
        if (!redisTemplate.opsForHash().hasKey(ContentConstantRedis.RECOMMEND,ContentConstantRedis.FIELD)){
            contents = tbContentMapper.getCommend();
            redisTemplate.opsForHash().put(ContentConstantRedis.RECOMMEND,ContentConstantRedis.FIELD,contents);
        }else {
            contents = (List<TbContent>)redisTemplate.opsForHash().get(ContentConstantRedis.RECOMMEND,ContentConstantRedis.FIELD);
        }
        return contents;
    }

    @Override
    public void updateContent(TbContent content) {
        tbContentMapper.updateByPrimaryKeySelective(content);
    }

    @Override
    public void deleteContent(Long[] ids) {
        tbContentMapper.deleteContent(ids);
    }
}

